package com.bbx.taxi.client.Task;

import android.app.Activity;
import android.app.Application;
import android.os.AsyncTask;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.passanger.WeightOrVolumnBuild;
import com.bbx.api.sdk.model.passanger.Return.WeightOrVolumn;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.VolumnNet;
import com.bbx.api.sdk.net.passeger.conn.WeightNet;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.DetailsOfGoodsActivity;
import com.bbx.taxi.client.Activity.login.LoginActivity;
import com.yanxuwen.loadview.LoadingDialog;

public class DetailsOfGoodsTask {
	private LoadingDialog dialog;
	private Activity context;
	private MyApplication mApplication;
	private WeightTask mWeightTask;
	private VolumnTask mVolumnTask;
	public static DetailsOfGoodsTask instance;

	public interface OnFinishListener {
		public void onFinish();
	}

	static OnFinishListener mOnFinishListener = null;

	public static void setOnFinishListener(OnFinishListener l) {
		mOnFinishListener = l;
	}

	public static DetailsOfGoodsTask getInstance(Activity context,
			MyApplication mApplication) {
		if (instance == null) {
			instance = new DetailsOfGoodsTask(context, mApplication);
			return instance;

		} else {
			if (mOnFinishListener != null) {
				mOnFinishListener.onFinish();
			}
			return instance;
		}
	}

	public DetailsOfGoodsTask(Activity context, MyApplication mApplication) {
		this.context = context;
		this.mApplication = mApplication;
		dialog = new LoadingDialog(context, Value.LOADDIALOGTIME);
		getWeight();
	}

	/**
	 * 获取重量
	 */
	public void getWeight() {
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
		WeightOrVolumnBuild mWeightOrVolumnBuild = new WeightOrVolumnBuild(context);
		mWeightOrVolumnBuild.uid = mApplication.getUid();
		mWeightOrVolumnBuild.access_token = mApplication.getToken();
		mWeightTask = new WeightTask(context, mWeightOrVolumnBuild);
		mWeightTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 获取体积
	 */
	public void getVolumn() {
		WeightOrVolumnBuild mWeightOrVolumnBuild = new WeightOrVolumnBuild(context);
		mWeightOrVolumnBuild.uid = mApplication.getUid();
		mWeightOrVolumnBuild.access_token = mApplication.getToken();
		mVolumnTask = new VolumnTask(context, mWeightOrVolumnBuild);
		mVolumnTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 重量
	 * 
	 */
	private class WeightTask extends BaseAsyncTask {
		private WeightOrVolumnBuild mWeightOrVolumnBuild;

		public WeightTask(Activity context,
				WeightOrVolumnBuild mWeightOrVolumnBuild) {
			super(context, false);
			setIsReturnString(true);
			this.mWeightOrVolumnBuild = mWeightOrVolumnBuild;
		}

		@Override
		public void onSuccess(int status,Object object) {
			// TODO Auto-generated method stub
			if (object != null) {
				SharedPreUtil.putStringValue(context,
						SharedPreEncryptUtil.weight_json, (String) object);
				// 存储静态变量Value.KJ_WEIGHT_0_5的值
			}
			getVolumn();
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context,
					context.getResources().getString(R.string.no_network));
			if (dialog != null) {
				dialog.onDismiss();
			}
			if (mOnFinishListener != null) {
				mOnFinishListener.onFinish();
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mWeightOrVolumnBuild)
					.getJson();
			return new WeightNet(context, json);
		}

	}

	/**
	 * 体积
	 * 
	 */
	private class VolumnTask extends BaseAsyncTask {
		private WeightOrVolumnBuild mWeightOrVolumnBuild;

		public VolumnTask(Activity context,
				WeightOrVolumnBuild mWeightOrVolumnBuild) {
			super(context, false);
			setIsReturnString(true);
			this.mWeightOrVolumnBuild = mWeightOrVolumnBuild;
		}

		@Override
		public void onSuccess(int status,Object object) {
			// TODO Auto-generated method stub
			if (object != null) {
				SharedPreUtil.putStringValue(context,
						SharedPreEncryptUtil.volume_json, (String) object);
			}
			if (dialog != null) {
				dialog.onDismiss();
			}
			if (mOnFinishListener != null) {
				mOnFinishListener.onFinish();
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context,
					context.getResources().getString(R.string.no_network));
			if (dialog != null) {
				dialog.onDismiss();
			}
			if (mOnFinishListener != null) {
				mOnFinishListener.onFinish();
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mWeightOrVolumnBuild)
					.getJson();
			return new VolumnNet(context, json);
		}

	}
}
